﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace PivotServerTools
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Globalization;
    using System.IO;
    using PivotServerTools.Internal;
    using System.Xml;
    using System.Xml.Schema;
    using System.Xml.Linq;
    using System.Xml.Xsl;
    using System.Xml.XPath;

    public class PeopleSearchResultCollection : Collection
    {

        private const string RES_NAME = "PivotServerTools.Resources.pivotcollection.xml";

        /* Internal properties   */
        private XDocument cxmlTree = null;
        
        private static XNamespace metadataNamespace = "http://schemas.microsoft.com/collection/metadata/2009";

        public string ImageBaseName
        {
            get
            {
                return base.ImgBaseName;
            }
        }


        internal override IList<CollectionItem> Items
        {
            get { 
                IList<CollectionItem> retVal = new List<CollectionItem>();
                

                XElement Items = cxmlTree.Root.Element(metadataNamespace + "Items");

                IEnumerable<CollectionItem> list1 = from el in Items.Elements(metadataNamespace + "Item")
                        select new CollectionItem() {
                    Name = el.Attribute("Name").Value,                   
                    Url =  FindLink(el),
                    ImageUri = el.Attribute("Href").Value,
                    Description = el.Attribute("Description") == null ? null : el.Attribute("Description").Value                        
                };

                return list1.ToList<CollectionItem>();
            }
        }


        private static string FindLink(XElement el){
            try
            {
                return el.Descendants(metadataNamespace + "Facet").Where(e => (string)e.Attribute("Name").Value == "Link")
                                        .FirstOrDefault().Attribute("Href").Value;
            }
            catch
            {
                return SPPivotSettings.DefaultImage;
            }
        }


        public PeopleSearchResultCollection(string CollectionName)
            : base()
        {
            base.SetDynamicDzc(CollectionName);
        }

        public void AddItems(XDocument cxmlTree)
        {
               this.cxmlTree = cxmlTree;
               // ValidateSchema();  
        }


        public override string ToCxml()
        {
            return cxmlTree.ToString(SaveOptions.None); //this option is easy for debugging
        }

        public override void ToCxml(TextWriter textWriter)
        {
            using(XmlTextWriter xmlWriter = new XmlTextWriter(textWriter))
            cxmlTree.WriteTo(xmlWriter);
        }

        public override void ToCxml(string filePath)
        {
            cxmlTree.Save(filePath);            
        }


        internal override string SetDynamicDzc(string collectionFileName)
        {            
            return this.CollectionKey;
        }

        /*        private void ValidateSchema()
                {
                    XmlSchemaSet schemas = new XmlSchemaSet();
                    using (Stream stream = typeof(PeopleSearchResultCollection).Assembly.GetManifestResourceStream(RES_NAME))
                    {
                        schemas.Add(null, XmlReader.Create(stream));                            
                        this.cxmlTree.Validate(schemas, (o, e) =>
                        {
                            throw new ApplicationException( e.Message);
                        });

                    }
                }*/

    }
}
